import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../http/api.dart';
import '../main.dart';

class IdBindDialog{
  String _name = '';
  String _id = '';

  idBindDialog(cardId){
    _name = '';
    _id = '';
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
        width: 180.w,
        height: 138.h,
        decoration:BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(8.r)),
        ),
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Positioned(
                top: 8.h,
                right: 12.w,
                child: InkWell(
                  onTap: (){
                    timer.cancel();
                    currentTask.clear();
                    Get.until((route) => route.isFirst);
                  },
                  child: Image.asset("assets/images/close.png",width: 13.w,fit: BoxFit.fitWidth,),
                )
            ),

            Positioned(
              top: 16.h,
              child: Text('身份证绑定',style: TextStyle(color:Colors.blue,fontWeight: FontWeight.bold,fontSize: 12.sp),),
            ),
            Positioned(
              top: 45.h,
              child:  SizedBox(
                width: 140.w,
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration:  InputDecoration(
                    hintText: '请输入真实姓名',
                    hintStyle: TextStyle(color: Colors.grey,fontSize: 6.sp),
                    enabledBorder:  OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
                  ),
                  style: TextStyle(fontSize: 8.sp),
                  onChanged: (value){
                    _name = value;
                  },
                ),
              ),
            ),
            Positioned(
              top: 72.h,
              child:  SizedBox(
                width: 140.w,
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration:  InputDecoration(
                    hintText: '请输入身份证号',
                    hintStyle: TextStyle(color: Colors.grey,fontSize: 6.sp),
                    enabledBorder:  OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(16.r)),
                      borderSide: BorderSide(color: Colors.blue, width: 0.3.w),
                    ),
                    contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
                  ),
                  style: TextStyle(fontSize: 8.sp),
                  onChanged: (value){
                    _id = value;
                  },
                ),
              ),
            ),
            Positioned(
                top: 104.h,
                child: InkWell(
                  onTap: () async {
                    var data = await readerApply(cardId, _name, _id);
                    if(data){
                      Get.back();
                      registerSuccessDialog();
                    }
                  },
                  child: Image.asset("assets/images/confirm.png",width: 80.w, fit: BoxFit.fitWidth,),
                )
            ),
          ],
        ),
      ),
    );
  }

  registerSuccessDialog(){
    tts.speak('办证成功');
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
          width: 200.w,
          height: 254.h,
          decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/register_success.png",
                ),
              )
          ),
          child: Stack(
            children: [
              Positioned(
                  top: 200.h,
                  left: 0.w,
                  right: 0.w,
                  child: InkWell(
                      onTap: (){
                        timer.cancel();
                        currentTask.clear();
                        Get.until((route) => route.isFirst);
                      },
                      child: Container(width:200.w,alignment:Alignment.center,
                        child: Text('办证成功',style: TextStyle(color: Colors.blue,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
                  )
              ),
              Positioned(
                  right: 10.w,
                  child: InkWell(
                    onTap: (){
                      timer.cancel();
                      currentTask.clear();
                      Get.until((route) => route.isFirst);
                    },
                    child: Container(
                        height: 31.h,
                        alignment: Alignment.center,
                        child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                    ),
                  )
              ),
            ],
          )
      ),
    );
  }

  registerFailedDialog(msg){
    tts.speak("身份证号已注册");
    Get.defaultDialog(
      title: "",
      barrierDismissible: false,
      contentPadding:EdgeInsets.zero,
      titlePadding:EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      content: Container(
          width: 200.w,
          height: 254.h,
          decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/register_failed.png",
                ),
              )
          ),
          child: Stack(
            children: [
              Positioned(
                  top: 200.h,
                  left: 0.w,
                  right: 0.w,
                  child: InkWell(
                      onTap: (){
                        timer.cancel();
                        currentTask.clear();
                        Get.until((route) => route.isFirst);
                      },
                      child: Container(width:200.w,alignment:Alignment.center,
                        child: Text('$msg',style: TextStyle(color: Colors.red,fontSize: 10.sp,fontWeight: FontWeight.bold),),)
                  )
              ),
              Positioned(
                  right: 10.w,
                  child: InkWell(
                    onTap: (){
                      timer.cancel();
                      currentTask.clear();
                      Get.until((route) => route.isFirst);
                    },
                    child: Container(
                        height: 31.h,
                        alignment: Alignment.center,
                        child: Icon(Icons.close,color: Colors.white,size: 20.sp,)
                    ),
                  )
              ),
            ],
          )
      ),
    );
  }
}